--
-- @Author:      feilong
-- @DateTime:    2020-10-30 23:05:48
-- @Description: 消息的派发

local pb = require "protobuf"
local skynet = require "skynet"
local PbLoader = class("PbLoader")

local table_insert = table.insert

function PbLoader:ctor()
	self.services = {}
	self:register()	
end

--注册本服务里的消息
function PbLoader:register()    
    self.message_dispatch = g.MsgDis()
end

--加载特定路径下的pb文件
function PbLoader:load()
    local pbfile = {}
    local server_config = dofile(skynet.getenv("server_config"))
    local pb_path = server_config.pb_path
    local pbdirs = {
        pb_path .. '/common',
        -- pb_path .. '/games',
        pb_path,
    }

    for _,pbdir in ipairs(pbdirs) do
        for filename in lfs.dir(pbdir) do
            if filename:find('%.pb$') then
                table_insert(pbfile,pbdir .. '/' ..  filename)
            end
        end
    end

    for _,filepath in pairs(pbfile) do
        print('registered file__start',filepath)
        pb.register_file(filepath)
        -- print('registered file__end',filepath)
    end
end

function PbLoader:encode(name, tb)
    return pb.encode(name, tb)
end
function PbLoader:decode(name, tb)
    return pb.decode(name, tb)
end

--使用例子
function PbLoader:test(name, tb)
	print('PbLoader test____________')
    local NoticeInfo = {
        id = 1,
        infotype = 2,
        gameid = 3,
        content = "ssssssssssss",
    }
    local data = pb.encode("user.NoticeInfo", NoticeInfo)
    print("__2222222_encode____",data)
    local decode_data = pb.decode("user.NoticeInfo", data)
    print(decode_data)
end

return PbLoader